
import math
import time
import uuid
import sys
import random

import os
os.chdir(os.path.dirname(__file__))
print("1",os.path.dirname(__file__))

import logs
import confhelper
from playwright.sync_api import Playwright, sync_playwright

import dbmapper
from entity import LinkMonResult, MonLinkBatch, MonLinkVersion, MonLink

logger = logs.get_logger('1688')

wiew_size = {"width": 1380, "height": 2240}
linkpath_prefix = confhelper.confdata().get("linkpath_prefix")+"1688/"
work_dir = confhelper.confdata().get("work_dir")

monLinks = []
page_size = 30

logger.info("sys.argv: {}".format(sys.argv))
version = sys.argv[1]

# linkid = 1854 这个商品爬取有问题，暂时手动跳过
# linkid = 1982，2004,2014，2061，2070 这个店铺一直加载，爬取有问题，暂时手动跳过
# 2071 报错，暂时跳过
# 2105，2118，2122,2125，2129，2237，2253,2256,2265,2310 一直等待，暂时跳过
def run(playwright: Playwright, link: MonLink) -> None:
    browser = playwright.firefox.launch(headless=False,slow_mo=3000,args=["--start-maximized"])
    context = browser.new_context(no_viewport=True,storage_state="1688_state.json")
    context.set_default_timeout(300000)
    page = context.new_page()
    page.set_default_timeout(300000)

    currentPage = link.currentPage + 1
    i = 0
    url = link.url
    linkId = link.id
    logger.info("开始遍历 url:{} id:{}".format(url,linkId))

    if 'https' not in url:
        url = 'https://' + url
    page.goto(url)
    page.wait_for_timeout(1000)
    passCheck(page)

    if page.locator("text=Error 404").count() > 0:
        logger.error("无效店铺: {}".format(url))
        dbmapper.MonLinkMapper.updateInvalid(MonLink(id=linkId))
        page.close()
        context.close()
        browser.close()
        return

    passCheck(page)

    with page.expect_popup() as page1_info:
        passCheck(page)
        page.get_by_text("全部商品").click()
    page1 = page1_info.value
    page1.wait_for_timeout(1000)

    passCheck(page1)

    pageTotal = page1.locator("#bd_1_container_0 > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > label:nth-child(1)").inner_text()
    logger.info(pageTotal)

    pageNum = math.ceil(int(pageTotal)/page_size)
    logger.info("url {} pageTotal {} pagenum: {} ".format(url,pageTotal,pageNum))

    dbmapper.MonLinkMapper.updateGoodSum(MonLink(id=linkId, goodSum=pageTotal))

    passCheck(page1)
    if currentPage > 1:
        page1.locator('#bd_1_container_0 input[type=number]').last.fill(str(currentPage))
        page1.locator('#bd_1_container_0 button').last.click()
    page1.wait_for_timeout(2000)
    pageLinks = page1.locator(".main-picture").all()
    if len(pageLinks) > 0 :
        view_page(currentPage,i,page1,pageLinks,url,linkId)

    logger.info("结束遍历 {}".format(url))

    if currentPage >= pageNum:
        monLinkUpdate = MonLink(version=version, id=linkId, currentPage=currentPage, totalPage=pageNum)
        dbmapper.MonLinkMapper.update_by_id(monLinkUpdate)
    else:
        monLinkUpdate = MonLink(version=(int(version) - 1), id=linkId, currentPage=currentPage, totalPage=pageNum)
        dbmapper.MonLinkMapper.update_by_id(monLinkUpdate)

    page1.close()
    page.close()
    context.close()
    browser.close()

def view_page(p_num:int,i:int,page,pageLinks,linkUrl,linkId):
    for p in pageLinks:
        i = i + 1
        j = 0
        logger.info("每页进度：{}/{}".format(i, len(pageLinks)))
        try:
            with page.expect_popup(timeout=60000) as popup_info:
                p.first.click()
            page1 = popup_info.value
            # page1.wait_for_load_state('networkidle')
            page1.wait_for_timeout(1000)

            passCheck(page1)
            # if page1.locator("text=商品详情").count() > 0 :
            if page1.locator("text=Sorry, we have detected unusual traffic from your network.").count() == 0:
                pic = linkpath_prefix +str(p_num)+"-"+str(i)+"-"+str(j)+page1.title().replace("*","").replace("/","#").replace(" ","").replace('"','').replace("|","-")[0:30]+".png"

                page1.locator("text=1688.com 版权所有").scroll_into_view_if_needed()
                # page1.evaluate("() => window.scrollTo(0,document.body.scrollHeight)")
                # page1.keyboard.down('End')
                # while page1.locator('.desc-img-no-load').last.is_visible():
                #     page1.mouse.wheel(0, 1000)

                page1.screenshot(path=pic,full_page=True)

                linkVersion=MonLinkVersion(linkId=linkId, linkUrl=linkUrl, goodUrl=page1.url,version=version,picPath="/profile"+pic.replace(work_dir,""))
                linkVersionCount=MonLinkVersion(linkId=linkId, linkUrl=linkUrl, version=version,picPath="/profile"+pic.replace(work_dir,""))
                lvCount = dbmapper.MonLinkVersionMapper.count(linkVersionCount)
                if int(lvCount) <= 0:
                    logger.info("新增数据。。。")
                    dbmapper.MonLinkVersionMapper.insert(linkVersion)
            else:
                logger.info("出现图形验证进入60秒睡眠")
                page1.wait_for_timeout(60000)

            page1.close()
        except Exception as e:
            logger.info("exception {} url:{}".format(i,page1.url))
            logger.exception("exception traceback is:{}".format(e))
            page1.close()
            if not str(e).startswith("Protocol error"):
                sys.exit()

def passCheck(page):
    while page.locator("text=Sorry, we have detected unusual traffic from your network.").count() > 0:
        logger.info("出现图形验证等待5秒")

        # if page.locator('.errloading').is_visible():
        #     page.locator('.errloading').click()
        # # 移动滑块
        # # 首先咱们的找到要移动的东西吧
        # s = page.wait_for_selector('#nc_1_n1z', strict=True)
        # # 找到这个元素再当前页面的坐标（这个会返回一个字典里边四个数字）
        # box = s.bounding_box()
        # # 移动鼠标到上边元素的中心（上边四个参数用途来了）
        # page.mouse.move(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2)
        # # 按下鼠标（这个不多说）
        # page.mouse.down()
        # logger.info("按下鼠标。。。")
        # # 这里获取到x坐标中心点位置
        # x = box["x"] + box["width"] / 2
        # logger.info("移动鼠标。。。")
        #
        # x = x + 260
        # steps = random.randint(1000,3000)
        # logger.info("steps：{}".format(steps))
        # page.mouse.move(x, 0, steps=steps)
        #
        # # i = 0
        # # array = get_random_trace()
        # # for m in array:
        # #     i = i + 1
        # #     x = x + m
        # #     if len(array) == i:
        # #         page.mouse.move(x, 0, steps=1000)
        # #     else:
        # #         page.mouse.move(x, 0, steps=100)
        # # 移动结束鼠标抬起
        # page.mouse.up()
        # logger.info("抬起鼠标。。。")
        page.wait_for_timeout(5000)

    page.wait_for_timeout(200)
    while page.locator(".baxia-dialog-content").is_visible():
        logger.info("出现图形验证等待5秒...")
        # page.locator('.baxia-dialog-close').click()
        page.wait_for_timeout(5000)

    page.wait_for_timeout(200)
    while page.locator('.J_MIDDLEWARE_FRAME_WIDGET').count() > 0:
        page.wait_for_timeout(5000)
        logger.info("出现图形验证等待5秒。。。")
        # if page.locator('.baxia-dialog-close').is_visible():
        #     # page.locator('.baxia-dialog-close').click()
        #     page.wait_for_timeout(200)

        # try:
        #     page.locator('.J_MIDDLEWARE_FRAME_WIDGET>img:nth-child(1)').last.click()
        # except Exception as e:
        #     logger.exception("exception traceback is:{}".format(e))
        page.wait_for_timeout(1000)
    page.wait_for_timeout(200)

# 256.4
def get_random_trace():
    total = 0
    array = []
    for i in range(1,10):
        a = random.randint(10, 35)
        total = total + a
        array.append(a)
        if i == 9 :
            array.insert(9, -10)
    array.append(270 - total)
    print(array)
    return array

os.chdir(os.path.dirname(__file__))
# 通过db加载url
monLink = dbmapper.MonLinkMapper.list_by_typea('1688', version)
if len(monLink) > 0:
    monLinks = monLink

logger.info("待监测的url %s",[(link.id,link.url) for link in monLinks])

currentI = 0
if len(monLinks) > 0:
    for link in monLinks:
        currentI = currentI + 1
        logger.info("整体进度：{}/{}".format(currentI, len(monLinks)))
        with sync_playwright() as playwright:
            run(playwright, link)
